package com.tencent.org.controller;


import com.tencent.basic.annotation.PreAuthorize;
import com.tencent.basic.util.AjaxResult;
import com.tencent.basic.util.PageList;
import com.tencent.org.domain.Employee;
import com.tencent.org.query.EmployeeQuery;
import com.tencent.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "用户的API",description="用户接口") // 打在Controller对Controller做描述 - swagger
@RestController // 复合注解 创建对象+json格式
@RequestMapping("/employee") //  匹配请求url - 等于是 http://localhost:8080/emp
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 接口 ： 添加或修改  put
     * http://localhost:8080/employee + {json对象} + put
     */
    @ApiOperation(value = "添加或修改" ) // 打在方法上，对方法做描述
    @PutMapping
    @PreAuthorize(name = "用户保存", sn = "employee:save")
    public AjaxResult addOrUpdate(@RequestBody Employee employee) {
        try {
            if (employee.getId() == null) {
                employeeService.save(employee);
            } else {
                employeeService.update(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败！").setSuccess(false);
        }
    }

    /**
     * 接口 ：删除用户  delete
     * http://localhost:8080/employee/{id} + 无 + delete
     */
    @ApiOperation(value = "删除用户" ) // 打在方法上，对方法做描述
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "用户删除", sn = "employee:delete")
    public AjaxResult deleteById(@PathVariable("id") Long id) {
        try {
            employeeService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败！");
        }
    }

    /**
     * 接口 ：查询所有用户  Get
     * http://localhost:8080/employee + 无 + Get
     */
    @ApiOperation(value = "查询所有用户" ) // 打在方法上，对方法做描述
    @GetMapping
    @PreAuthorize(name = "查询所有用户", sn = "employee:getAll")
    public List<Employee> queryAll() {
        return employeeService.queryAll();
    }

    /**
     * 接口 ：查询单个用户  Get
     * http://localhost:8080/employee/{id} + 无 + Get
     */
    @ApiOperation(value = "查询单个用户" ) // 打在方法上，对方法做描述
    @GetMapping("/{id}")
    @PreAuthorize(name = "查询单个用户", sn = "employee:getOne")
    public Employee queryById(@PathVariable("id") Long id) {
        return employeeService.queryById(id);
    }

    /**
     * 接口 ：分页查询  Get
     * http://localhost:8080/employee + {json对象} + Post
     */
    @ApiOperation(value = "分页查询" ) // 打在方法上，对方法做描述
    @PostMapping
    @PreAuthorize(name = "分页列表权限", sn = "employee:list")
    public PageList<Employee> queryList(@RequestBody EmployeeQuery emp) {
        return employeeService.queryPage(emp);
    }

    /**
     * 接口 ：批量删除  patch
     * http://localhost:8080/employee + {json对象} + delete
     */
    @ApiOperation(value = "批量删除" ) // 打在方法上，对方法做描述
    @PatchMapping
    @PreAuthorize(name = "批量删除用户", sn = "employee:patchDelete")
    // 前端传递过来的数组 我们既可以使用数组接收 也可以使用集合List<Long> 接收
    public AjaxResult patchDelete(@RequestBody Long[] ids) {
        try {
            employeeService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败！");
        }
    }

}
